package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-10-22
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {



    @Override
    public List<Brand> selectBrandList(String name) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name) ,Brand::getName,name);

        return baseMapper.selectList(wrapper);
    }

    @Override
    public void add(Brand brand) {
        // 封装品牌创建者id
        brand.setCreateBy(SecurityUtils.getUsername());
        // 根据品牌名字查询一下，看看品牌是否存在
        Long counts = count(new LambdaQueryWrapper<Brand>().eq(Brand::getName,brand.getName()));
        if(counts >0){
            throw new ServiceException("品牌名称已经存在了");
        }
        // 保存品牌
        save(brand);

    }
}
